package com.lzh.runke.core.entity.validator;

import com.lzh.runke.core.entity.validator.annotation.Regular;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author LiuZiHao
 * @date 创建时间:2023年07月24日 11:08:46
 */
public class RegularValidator implements ConstraintValidator<Regular, String> {

    private String regex;

    @Override
    public void initialize(Regular regular) {
        this.regex = regular.regex();
    }

    @Override
    public boolean isValid(String str, ConstraintValidatorContext constraintValidatorContext) {
        if (StringUtils.isBlank(str)) {
            return true;
        }
        return str.matches(regex);
    }
}
